<?php

namespace common\models\business;

use Yii;
use yii\db\Query;

/**
 * This is the model class for table "business_pig_price".
 *
 * @property string $id
 * @property integer $personid
 * @property string $person
 * @property string $pigno
 * @property integer $pigid
 * @property integer $createtime
 */
class BusinessPigPrice extends \yii\db\ActiveRecord
{


    //主要用于日志记录
    public $uselog = [
        'tabcomment' => '仔猪分割',      //用于修改了什么的显示
        'mainfield' => 'number',      //主要的显示标题的字段
        'picturefield' => [],        //图片字段
        'timefield' => [],       //时间字段
        'unsetfiled' =>[],      //不需要记录的字段
    ];






    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'business_pig_price';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['personid', 'pigid','grossweight','netweight','fgcost','number'], 'required'],
            [['personid', 'pigid', 'createtime'], 'integer'],
            [['grossweight','netweight','fgcost'], 'number'],
            [['person','number'], 'string', 'max' => 20],
            [['pigno'], 'string', 'max' => 50],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'personid' => '上传人',
            'person' => '数据上报人',
            'pigno' => '仔猪编号',
            'pigid' => '猪id',
            'createtime' => '分割时间',
            'grossweight' => '毛重（kg）',
            'netweight' => '净重(kg)',
            'fgcost' => '分割成本(元)',
            'number' => '分割编号'
        ];
    }


    public function beforeSave($insert)
    {
        if($this->isNewRecord){
            $this->createtime = time();
        }
        return parent::beforeSave($insert); // TODO: Change the autogenerated stub
    }


    /**
     * @return $this
     * 获取养殖农户
     */
    public function getFarmer(){
        return BusinessFarmers::find()->select('*')->alias('fa')
            ->leftJoin(BusinessPig::tableName().' as pi','pi.livery=fa.id')
            ->where('pi.id=:id',[':id'=>$this->pigid]);
    }

    /**
     *
     * 获取分割细项
     */
    public function getDetail()
    {
        return $this->hasMany(BusinessPigPriceDetail::className(),['pigpriceid'=>'id']);
    }
}
